package com.longrise.android.byjk.plugins.hra.hratabfirst.hradetailweb;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.utils.GetWebUrlHelper;
import com.longrise.common.base.BaseActivity2;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * Created by callxkj on 2018/1/30.
 * 用x5内核的一个webview
 */

public class HraDetailX5webActivity extends BaseActivity2 {

    private WebView mWb;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_hradetailx5web;
    }

    @Override
    public void initView() {
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                getWindow()
                        .setFlags(
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {
        }
        mWb = (WebView) findViewById(R.id.hradetail_x5_web);
        initWebSetting();
        GetWebUrlHelper.getWebUrl(GetWebUrlHelper.HRA, new GetWebUrlHelper.OnGetWebUrlResultListener() {
            @Override
            public void onSuccess(String url) {
                mWb.loadUrl(url);
            }

            @Override
            public void onError(String errormsg) {
                showToast(errormsg);
            }
        });
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }




    private void initWebSetting(){
        mWb.getView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
        WebSettings webSetting = mWb.getSettings();
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(false);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setAppCachePath(this.getDir("appcache", 0).getPath());
        webSetting.setDatabasePath(this.getDir("databases", 0).getPath());
        webSetting.setGeolocationDatabasePath(this.getDir("geolocation", 0)
                .getPath());
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);


        mWb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                setToolbarTitle(view.getTitle());
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWb.destroy();
    }


}
